package com.hanserwei.han_picture.websocket.message;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AI处理WebSocket消息
 *
 * @author hanserwei
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AiProcessMessage {

    /**
     * 消息类型
     */
    @Schema(description = "消息类型")
    private MessageType type;

    /**
     * 图片ID
     */
    @Schema(description = "图片ID")
    private Long pictureId;

    /**
     * 用户ID
     */
    @Schema(description = "用户ID")
    private Long userId;

    /**
     * 消息内容
     */
    @Schema(description = "消息内容")
    private String message;

    /**
     * AI生成的标题
     */
    @Schema(description = "AI生成的标题")
    private String aiTitle;

    /**
     * AI生成的分类
     */
    @Schema(description = "AI生成的分类")
    private String aiCategory;

    /**
     * AI生成的标签列表
     */
    @Schema(description = "AI生成的标签列表")
    private List<String> aiTags;

    /**
     * 错误信息
     */
    @Schema(description = "错误信息")
    private String error;

    /**
     * 时间戳
     */
    @Schema(description = "时间戳")
    private LocalDateTime timestamp;

    /**
     * 创建开始处理消息
     */
    public static AiProcessMessage createStartMessage(Long pictureId, Long userId) {
        AiProcessMessage message = new AiProcessMessage();
        message.setType(MessageType.AI_PROCESS_START);
        message.setPictureId(pictureId);
        message.setUserId(userId);
        message.setMessage("开始AI处理图片");
        message.setTimestamp(LocalDateTime.now());
        return message;
    }

    /**
     * 创建成功消息
     */
    public static AiProcessMessage createSuccessMessage(Long pictureId, Long userId,
                                                        String aiTitle, String aiCategory, List<String> aiTags) {
        AiProcessMessage message = new AiProcessMessage();
        message.setType(MessageType.AI_PROCESS_SUCCESS);
        message.setPictureId(pictureId);
        message.setUserId(userId);
        message.setMessage("AI处理完成");
        message.setAiTitle(aiTitle);
        message.setAiCategory(aiCategory);
        message.setAiTags(aiTags);
        message.setTimestamp(LocalDateTime.now());
        return message;
    }

    /**
     * 创建错误消息
     */
    public static AiProcessMessage createErrorMessage(Long pictureId, Long userId, String error) {
        AiProcessMessage message = new AiProcessMessage();
        message.setType(MessageType.AI_PROCESS_ERROR);
        message.setPictureId(pictureId);
        message.setUserId(userId);
        message.setMessage("AI处理失败");
        message.setError(error);
        message.setTimestamp(LocalDateTime.now());
        return message;
    }

    /**
     * 创建连接成功消息
     */
    public static AiProcessMessage createConnectionSuccessMessage(Long userId) {
        AiProcessMessage message = new AiProcessMessage();
        message.setType(MessageType.CONNECTION_SUCCESS);
        message.setUserId(userId);
        message.setMessage("WebSocket连接成功");
        message.setTimestamp(LocalDateTime.now());
        return message;
    }

    /**
     * 创建心跳消息
     */
    public static AiProcessMessage createHeartbeatMessage() {
        AiProcessMessage message = new AiProcessMessage();
        message.setType(MessageType.HEARTBEAT);
        message.setMessage("heartbeat");
        message.setTimestamp(LocalDateTime.now());
        return message;
    }

    /**
     * 消息类型枚举
     */
    public enum MessageType {
        /**
         * AI处理开始
         */
        AI_PROCESS_START,

        /**
         * AI处理成功
         */
        AI_PROCESS_SUCCESS,

        /**
         * AI处理失败
         */
        AI_PROCESS_ERROR,

        /**
         * 连接成功
         */
        CONNECTION_SUCCESS,

        /**
         * 心跳
         */
        HEARTBEAT
    }
}
